# -------------------------------------------------------------------------------------- #
# -------------------------------- 工程配置 --------------------------------------------- #
# -------------------------------------------------------------------------------------- #
#CMake版本要求
cmake_minimum_required(VERSION 3.20)
#cpp语言标准
set(CMAKE_CXX_STANDARD 17)
#工程名称
project(LearnOpenMP VERSION 2023.0)

set(EXE_NAME "Learn") # 主程序名

# 编译选项: 具体参考各自的编译器
if(MSVC)
    message(STATUS "Visual studio compiler options!")
    # add_compile_options(-openmp:llvm)
    add_compile_options(-wd4819 -wd4996) # 忽略警告4819和4996
    # add_compile_options(/bigobj)
endif()

# 输出的静态库、动态库、EXE目录
message(STATUS "CMake Directory:" ${CMAKE_BINARY_DIR})
set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/install")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib") # 输出的静态库目录
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") # 输出的动态库目录
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") # 输出的EXE目录

# -------------------------------------------------------------------------------------- #
# ---------------------------- 管理头文件 ------------------------------------------- #
# -------------------------------------------------------------------------------------- #
include_directories(${CMAKE_CURRENT_SOURCE_DIR})

###### MKL OpenMP库 #######
set(PARALLEL_ENABLE TRUE) # 设置串行（FALSE）或并行（TRUE）
set(oneAPI_COMPILER_PATH "D:/Program Files (x86)/Intel/oneAPI/compiler/latest/windows/compiler")
set(MKL_ROOT $ENV{ONEAPI_ROOT})
message(STATUS "${MKL_ROOT}")
# MKL库头文件
include_directories("${oneAPI_COMPILER_PATH}/include")
if(PARALLEL_ENABLE)
    # 并行版的库
    message(STATUS "Using parallel MKL OpenMP")
    # find_package(OpenMP REQUIRED)
    set(MKL_LIB_OpenMP
            ${oneAPI_COMPILER_PATH}/lib/intel64_win/libiomp5md.lib
            )
else()
    # 串行版的库
    message(STATUS "Using sequential MKL")
    set(MKL_LIB_OpenMP
            )
endif()

message(STATUS "OpenMP: ${MKL_LIB_OpenMP}")
# 将依赖的动态库复制到运行文件夹中
if(FALSE) # TRUE需要复制；FALSE不需要复制
    file(COPY ${MKL_ROOT}mkl/latest/redist/intel64/mkl_core.2.dll
            DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
endif()

# -------------------------------------------------------------------------------------- #
# --------------------------- 解决方案/可执行程序 ----------------------------------------- #
# -------------------------------------------------------------------------------------- #
set(SRC src/ex3.cpp)

add_executable(${EXE_NAME} ${SRC})

if(PARALLEL_ENABLE)
    # 并行版编译编译选项
    target_compile_options(${EXE_NAME} PRIVATE /openmp)
    if (${CMAKE_BUILD_TYPE} STREQUAL Debug)
        message(STATUS "Parallel debug")
        target_link_libraries(${EXE_NAME}
                # PUBLIC ${LIB}
                PUBLIC debug  # PRIVATE/PUBLIC
                ${MKL_LIB_OpenMP} # OpenMP::OpenMP_CXX
                )
    else ()
        message(STATUS "Parallel release")
        target_link_libraries(${EXE_NAME}
                # PUBLIC ${LIB}
                PUBLIC optimized ${MKL_LIB_OpenMP}  # OpenMP::OpenMP_CXX
                )
    endif ()
else()
    message(STATUS "Sequential")
    target_link_libraries(${EXE_NAME}
            # PUBLIC ${LIB}
            )
endif()





